<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\bootstrap\ActiveForm;
use app\modules\Order\models\OrderCustomer;
use kartik\editable\Editable;
use app\modules\Order\models\Leader;
//use kartik\select2\Select2;

$this->title = $product->name . '  ' . $product->go_time;


//获取领队信息
$leaders = Leader::find()->where(['status'=>1])->all();
$lead = [];
if(!empty($leaders)){
	foreach($leaders as $v){
		$lead[$v->id] = $v->first_name.$v->last_name;
	}
}

?>
<style type="text/css">
	.order-view font {color:#9E9A9A;margin-right:10px;}
</style>

<div class="order-view">
    <h2 class="page-header"><small><?= Html::encode($this->title) ?></small></h2>

	<p class="bg-danger" style="padding:15px;"> 
		<?= $product->allOrderNum ?> 条订单  /  共计 <?php print_r($product->allCustomerNum); ?> 人
	</p>

	<hr>

	<div>
		<a data-toggle="modal" data-target=".leader-modal"  href="/order/leader/create-new" class="btn btn-primary">创建新领队</a>	
		<a class="btn btn-danger" href="/order/leader/index" target="__blank" >
			领队列表
		</a>	
		<a class="btn btn-default" href="<?= sprintf("/order/order/assign-save?OrderAssign[product_id]=%d", $product->id) ?>" >
			添加新团
		</a>		
	</div>

	<div class="main-num" id="main-num" style="margin-top:30px;">
	<!-- div class="bg-success text-center center" style="width:80px;height:80px;line-height:80px;margin:15px;"> 
		<a>创建团队</a>
	</div -->
	<?php if(isset($assign) && !empty($assign)){ ?>
		<?php foreach($assign as $model){ ?>
			<div class="col-md-2 bg-success text-center assign-btn" style="padding:20px 10px;margin:0 15px 15px 0;height: 200px;">
				<div><h4>
					<!-- ?php echo $model->name; ? -->
    				<?php echo Editable::widget([
    				    'name' => 'OrderAssign[name]',
						'value' => $model->name,
    				    'asPopover' => true,
    				    'header' => '团队名称',
    				    'size'=>'md',
    				    'formOptions' => ['action'=>'/order/order/assign-name?id='.$model->id,'method'=>'get'],
    				    'options' => ['class'=>'form-control', 'placeholder'=>'请输入团队名称'],
    				]);?>
					</h4>
				</div>
				<div><h5>已分配 <?= $model->orderCount ?> 单/ 共 <?= $model->orderPeopleCount ?> 人</h5></div>
				<div>
					<?php $url = sprintf("/order/order/assign-user?product_id=%d&assign_id=%d", $product->id, $model->id); ?>

					<?= Html::a('&nbsp;', $url,['data-toggle'=>'modal', 'data-target'=>'.assign-modal', 'class'=>'glyphicon glyphicon-user ']); ?>
					<?= Html::a('&nbsp;', ['/order/order/assign-empty', 'assign_id'=>$model->id, 'product_id'=>$product->id], [
							'class' => 'glyphicon glyphicon-trash',
							'data' => [
								'confirm' => '确定清空该团期的内容吗?',
								'method' => 'post',
							],
					]) ?>

					<?= Html::a('&nbsp;', ['/order/order/assign-remove', 'assign_id'=>$model->id, 'product_id'=>$product->id], [
							'class' => 'glyphicon glyphicon-remove',
							'data' => [
								'confirm' => '确定删除该团期吗?',
								'method' => 'post',
							],
					]) ?>
				</div>
				<div style="margin-top: 10px;">
				<?php 
			 		echo kartik\select2\Select2::widget([
				    		'name' => 'lead_'.$model->id,
				    		'data' => $lead,
				    		'value' => $model->leader_id,
				    		'options' => [
				    		    'placeholder' => '选择领队',
				    		    'class' => 'assign_lead  lead_'.$model->id,
				    		    'id' => 'lead_'.$model->id,
				    		    'data-assign' => $model->id
				    		],
							'pluginOptions' => [
								'tags' => true,
								'maximumInputLength' => 10
							],
						]);
   				?>
   				</div>
			</div>
		<?php } ?>
	<?php } ?>
	</div>

</div>

<div class="modal fade assign-modal"   role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document">
      <div class="modal-content">

	 		<!-- 嵌入内容 --> 
	  </div>
  </div>
</div>

<div class="modal fade leader-modal"   role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document">
      <div class="modal-content">
	 		<!-- 嵌入内容 --> 
	  </div>
  </div>
</div>





<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">
// 清除模态框缓存
$(".assign-modal").on("hidden.bs.modal", function() {
	$(this).removeData("bs.modal");
	window.location.reload();
});

 //保存领队对应的分团	
 $(".assign_lead").change(function(){
        var lead_id = $(this).val();
	    var assign_id = $(this).attr("data-assign");
	   	$.get("/order/customer/save-leader", {"assign_id":assign_id,"lead_id":lead_id}, function(data){
            if(data){
                console.log(data);
            }
    	});
       
  });
</script>
<?php app\components\JsBlock::end() ?>
